const initialState = {
  list: [
    {
      id: 1,
      task: 'React',
      isDone: false,
    },
    {
      id: 2,
      task: 'React2',
      isDone: true,
    },
    {
      id: 3,
      task: 'React3',
      isDone: false,
    },
  ],
  type: 'all',
};

export default function reducer(state = initialState, { type, payload }) {
  switch (type) {
    // 2.1. 更新的逻辑分支
    case 'update':
      return {
        ...state,
        // 2.4 编写更新的逻辑
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === payload ? !item.isDone : item.isDone,
        })),
      };
    // 3.1 reduer新建逻辑分支
    case 'delById':
      // 3.4 编写删除的逻辑
      return {
        ...state,
        list: state.list.filter((item) => item.id !== payload),
      };

    // 4.1 新建分支
    case 'add':
      console.log('payload  ----->  ', payload);
      return {
        ...state,
        // 4.6 完成新增的逻辑
        list: [...state.list, payload],
      };

    default:
      return state;
  }
}
